package com.googlecode.marrowboy.it;

import java.io.File;

import org.apache.maven.it.Verifier;
import org.apache.maven.it.util.ResourceExtractor;
import org.junit.Test;

public class MultipleOutputFilesOfDifferentFormatsIT extends AbstractITClass {

    @Test
    public void multipleOutputFilesIt() throws Exception {

        File testDir = ResourceExtractor.simpleExtractResources(getClass(), "/multiple-output-files-it");

        Verifier verifier = new Verifier(testDir.getAbsolutePath());
        this.executeGoal(verifier, "test");
        verifier.verifyErrorFreeLog();
        verifier.resetStreams();

        assertMavenLogRefersToBothFiles(testDir);
        checkXmlOutputIsPresentAndCorrect(testDir);
        checkTxtOutputIsPresentAndCorrect(testDir);
    }

    private void checkTxtOutputIsPresentAndCorrect(File testDir) {
        File txtFile = new File(testDir, "target/marrowboy/txt/txtReport.txt");
        assertFileContains(txtFile, "\tSimple test");
        assertFileContains(txtFile, "\t\tSimple method");
    }

    private void checkXmlOutputIsPresentAndCorrect(File testDir) {
        File xmlFile = new File(testDir, "target/marrowboy/xml/xmlReport.xml");
        assertFileContains(xmlFile, "<class name=\"Simple test\">");
        assertFileContains(xmlFile, "<method name=\"Simple method\">");
    }

    private void assertMavenLogRefersToBothFiles(File testDir) {
        File mavenStdOut = new File(testDir, "log.txt");
        assertFileHasMatch(mavenStdOut, "^\\[INFO\\] MarrowBoy report file: .+xmlReport.xml$");
        assertFileHasMatch(mavenStdOut, "^\\[INFO\\] MarrowBoy report file: .+txtReport.txt$");
    }

}
